package com.atguigu.commonutils.utils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

@Data
@Accessors(chain = true)
public class LoginUser implements java.io.Serializable {

    /**
     * id
     */
    @ApiModelProperty("id")
    private String id;

    /**
     * 用户登录名
     */
    @ApiModelProperty("用户登录名")
    private String phone;

    /**
     * 用户名字
     */
    @ApiModelProperty("用户名字")
    private String name;

    /**
     * 状态(1:启用,0:禁用)
     */
    @ApiModelProperty("状态(1:启用,0:禁用)")
    private Integer status;


    /**
     * 最后一次登录时间毫秒
     */
    @ApiModelProperty("用户名字")
    private Long lastLoginTime;

    /**
     * openid
     */
    @ApiModelProperty("openid")
    private String openid;

    /**
     * 头像
     */
    @ApiModelProperty("avatar")
    private String avatar;


    /**
     * 登录成功,将用户信息存储在redis中
     */
    public boolean login() {
        RedisTemplate<String, LoginUser> jsonRedisTemplate = (RedisTemplate<String, LoginUser>) SpringContextUtil.getBean("jsonRedisTemplate");
        jsonRedisTemplate.opsForValue().set(String.format("alan:loginUser:%s", this.getId()), this, Const.JWT_EXPIRATION, TimeUnit.SECONDS);
        return true;
    }

    /**
     * 登出成功,将用户信息从redis中移除
     */
    public boolean logout() {
        RedisTemplate<String, LoginUser> jsonRedisTemplate = (RedisTemplate<String, LoginUser>) SpringContextUtil.getBean("jsonRedisTemplate");
        return jsonRedisTemplate.delete(String.format("alan:loginUser:%s", this.getId()));
    }
}
